package com.fyzk.utils.jc;

import com.fyzk.model.JCLQMatchData;

import java.util.ArrayList;

/**
 * Created by miao on 2017/8/17.
 */
public class JLHGGroup {

    JCLQMatchData info;

    class SFC {
        public int min;
        public int max;

        public SFC(int min, int max) {
            this.min = min;
            this.max = max;
        }
    }

    SFC[] sfcOkeys_ks = new SFC[]{new SFC(-5, -1), new SFC(-10, -6), new SFC(-15, -11), new SFC
            (-20, -16), new SFC(-25, -21), new SFC(-26, -100)};
    SFC[] sfcOkeys_zs = new SFC[]{new SFC(1, 5), new SFC(6, 10), new SFC(11, 15), new SFC(16, 20)
            , new SFC(21, 25), new SFC(26, 100)};

    String[] sfcAkeys_ks;
    String[] sfcAkeys_zs;
    String[] rfsfAkeys;
    String[] sfAkeys;
    String[] dxfAkeys;

    public JLHGGroup(JCLQMatchData info) {

        this.info = info;

        sfcAkeys_ks = new String[]{info.SFC_11, info.SFC_12, info.SFC_13, info.SFC_14, info
                .SFC_15, info.SFC_16};
        sfcAkeys_zs = new String[]{info.SFC_01, info.SFC_02, info.SFC_03, info.SFC_04, info
                .SFC_05, info.SFC_06};
        rfsfAkeys = new String[]{info.LETVS_3, info.LETVS_0};
        sfAkeys = new String[]{info.VS_3, info.VS_0};
        dxfAkeys = new String[]{info.DXF_1, info.DXF_2};
    }


    public ArrayList<ArrayList<String>> hgGroup() {

        ArrayList<ArrayList<String>> situation = new ArrayList<>();
        String handipcap = info.getHandicap();
        double handicap = Double.parseDouble(handipcap.replace("+",""));
        JCLQMatchData.SPInfo letvs_3_sp = info.splInfos.get(info.LETVS_3);
        JCLQMatchData.SPInfo letvs_0_sp = info.splInfos.get(info.LETVS_0);
        JCLQMatchData.SPInfo vs_3_sp = info.splInfos.get(info.VS_3);
        JCLQMatchData.SPInfo vs_0_sp = info.splInfos.get(info.VS_0);

        ArrayList<String> dxf_sels = new ArrayList<>();
        ArrayList<String> letvs_sels = new ArrayList<>();
        ArrayList<String> vs_sels = new ArrayList<>();

        for (String dxf : dxfAkeys) {
            if (info.splInfos.get(dxf).isSel) {
                dxf_sels.add(dxf);
            }
        }
        for (String rfsf : rfsfAkeys) {
            if (info.splInfos.get(rfsf).isSel) {
                letvs_sels.add(rfsf);
            }
        }
        for (String sf : sfAkeys) {
            if (info.splInfos.get(sf).isSel) {
                vs_sels.add(sf);
            }
        }
        boolean nosfcsel = true;//没有选择胜分差玩法
        if (letvs_3_sp.isSel || letvs_0_sp.isSel) {
            if (handicap > 0) {//让分为正
                /***********************************************************************************************************************/
                /*[-26].[-25__-21].[-20__-16].[-15__-11].[-10__-6]..........[-5__-1]-|-[1__5].[6__10].[11__15].[16__20].[21__25].[26+]*/
                /*                                          |~~~~~  VS_0         ~~~~|                                                */
                /*                                          |~~~~  LETVS_3        ~~~|                                                */
                /*                                          |<-SFC[-10-6] SFC[-5-1]->|                                                */
                /* <--             VS_0                  -->|                        |                                                */
                /* <--             LETVS_0               -->|                        |                                                */
                /* <--SFC[-26]..........>.....SFC[-10,-6]-->|                        |                                                */
                /*                                                                   |<---          VS_3                         ---> */
                /*                                                                   |<---          LETVS_3                      ---> */
                /*                                                                   |<---SFC[1,5] .....................SFC[26+] ---> */
                /**********************************************************************************************************************/
                int index = 0;
                for (String key : sfcAkeys_ks) {//客胜
                    if (info.splInfos.get(key).isSel) {//胜分差是否被选择
                        if (nosfcsel) {
                            nosfcsel = false;
                        }
                        SFC sfc = sfcOkeys_ks[index];
                        double sum_min = sfc.min + handicap;
                        double sum_max = sfc.max + handicap;
                        if (dxf_sels.size() > 0) {//大小分有被选择
                            for (String dxkey : dxf_sels) {
                                if (sum_min > 0) {
                                    ArrayList<String> sel_keys = new ArrayList<>();
                                    sel_keys.add(key);
                                    sel_keys.add(dxkey);
                                    if (vs_0_sp.isSel) {
                                        sel_keys.add(info.VS_0);
                                    }
                                    if (letvs_3_sp.isSel) {
                                        sel_keys.add(info.LETVS_3);
                                    }
                                    situation.add(sel_keys);
                                } else if (sum_min < 0 && sum_max > 0) {
                                    for (String letv : letvs_sels) {
                                        ArrayList<String> sel_keys = new ArrayList<>();
                                        sel_keys.add(key);
                                        sel_keys.add(dxkey);
                                        if (vs_0_sp.isSel) {
                                            sel_keys.add(info.VS_0);
                                        }
                                        sel_keys.add(letv);
                                        situation.add(sel_keys);
                                    }
                                } else {
                                    ArrayList<String> sel_keys = new ArrayList<>();
                                    sel_keys.add(key);
                                    sel_keys.add(dxkey);
                                    if (vs_0_sp.isSel) {
                                        sel_keys.add(info.VS_0);
                                    }
                                    if (letvs_0_sp.isSel) {
                                        sel_keys.add(info.LETVS_0);
                                    }
                                    situation.add(sel_keys);
                                }

                            }
                        } else {//大小分没有被选择
                            if (sum_min > 0) {
                                ArrayList<String> sel_keys = new ArrayList<>();
                                sel_keys.add(key);
                                if (vs_0_sp.isSel) {
                                    sel_keys.add(info.VS_0);
                                }
                                if (letvs_3_sp.isSel) {
                                    sel_keys.add(info.LETVS_3);
                                }
                                situation.add(sel_keys);
                            } else if (sum_min < 0 && sum_max > 0) {
                                for (String letv : letvs_sels) {
                                    ArrayList<String> sel_keys = new ArrayList<>();
                                    sel_keys.add(key);
                                    if (vs_0_sp.isSel) {
                                        sel_keys.add(info.VS_0);
                                    }
                                    sel_keys.add(letv);
                                    situation.add(sel_keys);
                                }
                            } else {
                                ArrayList<String> sel_keys = new ArrayList<>();
                                sel_keys.add(key);
                                if (vs_0_sp.isSel) {
                                    sel_keys.add(info.VS_0);
                                }
                                if (letvs_0_sp.isSel) {
                                    sel_keys.add(info.LETVS_0);
                                }
                                situation.add(sel_keys);
                            }
                        }
                    }
                    index++;
                }
                for (String key : sfcAkeys_zs) {//主胜
                    if (info.splInfos.get(key).isSel) {//胜分差是否被选择
                        if (nosfcsel) {
                            nosfcsel = false;
                        }
                        if (dxf_sels.size() > 0) {//大小分有被选择
                            for (String dxkey : dxf_sels) {
                                ArrayList<String> sel_keys = new ArrayList<>();
                                sel_keys.add(key);
                                sel_keys.add(dxkey);
                                if (vs_3_sp.isSel) {
                                    sel_keys.add(info.VS_3);
                                }
                                if (letvs_3_sp.isSel) {
                                    sel_keys.add(info.LETVS_3);
                                }
                                situation.add(sel_keys);
                            }
                        } else {//大小分没有被选择
                            ArrayList<String> sel_keys = new ArrayList<>();
                            sel_keys.add(key);
                            if (vs_3_sp.isSel) {
                                sel_keys.add(info.VS_3);
                            }
                            if (letvs_3_sp.isSel) {
                                sel_keys.add(info.LETVS_3);
                            }
                            situation.add(sel_keys);
                        }
                    }
                }
            } else {////让分为负
                /***********************************************************************************************************************/
                /*[-26].[-25__-21].[-20__-16].[-15__-11].[-10__-6][-5__-1]-|-[1__5]............[6__10].[11__15].[16__20].[21__25].[26+]*/
                /*                                                         |~~~~~ VS_3           ~~~|                                  */
                /*                                                         |~~~~~ LETVS_0        ~~~|                                  */
                /*                                                         |<--SFC[1,5].SFC[6,10]-->|                                  */
                /*                                                         |                        |<---             VS_3        ---> */
                /*                                                         |                        |<---          LETVS_3        ---> */
                /*                                                         |                        |<---   SFC[6,10] ....SFC[26+]---> */
                /* <--                     VS_0                        --> |                                                           */
                /* <--                    LETVS_0                      --> |                                                           */
                /* <--SFC[-26]..............................SFC[-5,-1] --> |                                                           */
                /***********************************************************************************************************************/
                int index = 0;
                for (String key : sfcAkeys_zs) {//主胜
                    if (info.splInfos.get(key).isSel) {//胜分差是否被选择
                        if (nosfcsel) {
                            nosfcsel = false;
                        }
                        SFC sfc = sfcOkeys_zs[index];
                        double sum_min = sfc.min + handicap;
                        double sum_max = sfc.max + handicap;
                        if (dxf_sels.size() > 0) {//大小分有被选择
                            for (String dxkey : dxf_sels) {
                                if (sum_max < 0) {
                                    ArrayList<String> sel_keys = new ArrayList<>();
                                    sel_keys.add(key);
                                    sel_keys.add(dxkey);
                                    if (vs_3_sp.isSel) {
                                        sel_keys.add(info.VS_3);
                                    }
                                    if (letvs_0_sp.isSel) {
                                        sel_keys.add(info.LETVS_0);
                                    }
                                    situation.add(sel_keys);
                                } else if (sum_min < 0 && sum_max > 0) {
                                    for (String letvs : letvs_sels) {
                                        ArrayList<String> sel_keys = new ArrayList<>();
                                        sel_keys.add(key);
                                        sel_keys.add(dxkey);
                                        if (vs_3_sp.isSel) {
                                            sel_keys.add(info.VS_3);
                                        }
                                        sel_keys.add(letvs);
                                        situation.add(sel_keys);
                                    }
                                } else {
                                    ArrayList<String> sel_keys = new ArrayList<>();
                                    sel_keys.add(key);
                                    sel_keys.add(dxkey);
                                    if (vs_3_sp.isSel) {
                                        sel_keys.add(info.VS_3);
                                    }
                                    if (letvs_3_sp.isSel) {
                                        sel_keys.add(info.LETVS_3);
                                    }
                                    situation.add(sel_keys);
                                }
                            }
                        } else {//大小分没有被选择
                            if (sum_max < 0) {
                                ArrayList<String> sel_keys = new ArrayList<>();
                                sel_keys.add(key);
                                if (vs_3_sp.isSel) {
                                    sel_keys.add(info.VS_3);
                                }
                                if (letvs_0_sp.isSel) {
                                    sel_keys.add(info.LETVS_0);
                                }
                                situation.add(sel_keys);
                            } else if (sum_min < 0 && sum_max > 0) {
                                for (String letvs : letvs_sels) {
                                    ArrayList<String> sel_keys = new ArrayList<>();
                                    sel_keys.add(key);
                                    if (vs_3_sp.isSel) {
                                        sel_keys.add(info.VS_3);
                                    }
                                    sel_keys.add(letvs);
                                    situation.add(sel_keys);
                                }
                            } else {
                                ArrayList<String> sel_keys = new ArrayList<>();
                                sel_keys.add(key);
                                if (vs_3_sp.isSel) {
                                    sel_keys.add(info.VS_3);
                                }
                                if (letvs_3_sp.isSel) {
                                    sel_keys.add(info.LETVS_3);
                                }
                                situation.add(sel_keys);
                            }
                        }
                    }
                    index++;
                }

                for (String key : sfcAkeys_ks) {//主胜
                    if (info.splInfos.get(key).isSel) {//胜分差是否被选择
                        if (nosfcsel) {
                            nosfcsel = false;
                        }
                        if (dxf_sels.size() > 0) {//胜分差+大小分+胜负+让分胜负
                            for (String dxkey : dxf_sels) {
                                ArrayList<String> sel_keys = new ArrayList<>();
                                sel_keys.add(key);
                                sel_keys.add(dxkey);
                                if (vs_0_sp.isSel) {
                                    sel_keys.add(info.VS_0);
                                }
                                if (letvs_0_sp.isSel) {
                                    sel_keys.add(info.LETVS_0);
                                }
                                situation.add(sel_keys);
                            }
                        } else {////胜分差+胜负+让分胜负
                            ArrayList<String> sel_keys = new ArrayList<>();
                            sel_keys.add(key);
                            if (vs_0_sp.isSel) {
                                sel_keys.add(info.VS_0);
                            }
                            if (letvs_0_sp.isSel) {
                                sel_keys.add(info.LETVS_0);
                            }
                            situation.add(sel_keys);
                        }
                    }
                }

            }
        } else {
            for (String key : sfcAkeys_ks) {
                if (info.splInfos.get(key).isSel) {
                    if (nosfcsel) {
                        nosfcsel = false;
                    }
                    if (dxf_sels.size() > 0) {//胜分差+大小分+胜负
                        for (String dxkey : dxf_sels) {
                            ArrayList<String> sel_keys = new ArrayList<>();
                            sel_keys.add(key);
                            sel_keys.add(dxkey);
                            if (info.splInfos.get(info.VS_0).isSel) {
                                sel_keys.add(info.VS_0);
                            }
                            situation.add(sel_keys);
                        }
                    } else {//胜分差+胜负
                        ArrayList<String> sel_keys = new ArrayList<>();
                        sel_keys.add(key);
                        if (info.splInfos.get(info.VS_0).isSel) {
                            sel_keys.add(info.VS_0);
                        }
                        situation.add(sel_keys);
                    }
                }

            }
            for (String key : sfcAkeys_zs) {
                if (info.splInfos.get(key).isSel) {
                    if (nosfcsel) {
                        nosfcsel = false;
                    }
                    if (dxf_sels.size() > 0) {//胜分差+大小分+胜负
                        for (String dxkey : dxf_sels) {
                            ArrayList<String> sel_keys = new ArrayList<>();
                            sel_keys.add(key);
                            sel_keys.add(dxkey);
                            if (info.splInfos.get(info.VS_3).isSel) {
                                sel_keys.add(info.VS_3);
                            }
                            situation.add(sel_keys);
                        }
                    } else {//胜分差+胜负
                        ArrayList<String> sel_keys = new ArrayList<>();
                        sel_keys.add(key);
                        if (info.splInfos.get(info.VS_3).isSel) {
                            sel_keys.add(info.VS_3);
                        }
                        situation.add(sel_keys);
                    }
                }
            }
        }


        if (nosfcsel) {
            if (dxf_sels.size() > 0) {
                if (letvs_sels.size() > 0) {
                    for (String rfsf : letvs_sels) {
                        for (String dxkey : dxf_sels) {
                            if (vs_sels.size() > 0) {//大小分 + 让分胜负+胜负
                                for (String sf : vs_sels) {
                                    ArrayList<String> sel_keys = new ArrayList<>();
                                    sel_keys.add(dxkey);
                                    sel_keys.add(sf);
                                    sel_keys.add(rfsf);
                                    situation.add(sel_keys);
                                }
                            } else {//大小分 + 让分胜负
                                ArrayList<String> sel_keys = new ArrayList<>();
                                sel_keys.add(dxkey);
                                sel_keys.add(rfsf);
                                situation.add(sel_keys);
                            }
                        }
                    }
                } else {
                    for (String dxkey : dxf_sels) {
                        if (vs_sels.size() > 0) {//大小分+胜负
                            for (String sf : vs_sels) {
                                ArrayList<String> sel_keys = new ArrayList<>();
                                sel_keys.add(dxkey);
                                sel_keys.add(sf);
                                situation.add(sel_keys);
                            }
                        } else {//大小分
                            ArrayList<String> sel_keys = new ArrayList<>();
                            sel_keys.add(dxkey);
                            situation.add(sel_keys);
                        }
                    }
                }
            } else {
                if (letvs_sels.size() > 0) {
                    for (String rfsf : letvs_sels) {
                        if (vs_sels.size() > 0) {//胜负+让分胜负
                            for (String sf : vs_sels) {
                                ArrayList<String> sel_keys = new ArrayList<>();
                                sel_keys.add(sf);
                                sel_keys.add(rfsf);
                                situation.add(sel_keys);
                            }
                        } else {//让分胜负
                            ArrayList<String> sel_keys = new ArrayList<>();
                            sel_keys.add(rfsf);
                            situation.add(sel_keys);
                        }
                    }
                } else {
                    if (vs_sels.size() > 0) {//胜负
                        for (String sf : vs_sels) {
                            ArrayList<String> sel_keys = new ArrayList<>();
                            sel_keys.add(sf);
                            situation.add(sel_keys);
                        }
                    }
                }
            }
        }
        return situation;
    }

}
